﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaopxFramework.Common.Method;
using BaopxFramework.DAL;

namespace BaopxFramework.Admin.ManageTaxonomy
{
    public partial class ManageTaxonomy : System.Web.UI.Page
    {
        private List<Taxonomy> _allTaxonomyParents; 

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadData();
        }

        private void LoadData()
        {
            using (var d = new Repository())
            {
                var allTaxonomies = d.GetTaxonomys(0);
                _allTaxonomyParents = allTaxonomies.Where(i => i.ParentId == null).ToList();
                _allTaxonomyParents.Insert(0,new Taxonomy{Name = "",Id = 0});
                grd.DataSource = allTaxonomies;
                grd.DataBind();
            }
        }

        protected void grd_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            switch (e.Item.ItemType)
            {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                case ListItemType.Footer:
                    var cboParent = (DropDownList) e.Item.FindControl("cboParent");
                    cboParent.DataSource = _allTaxonomyParents;
                    cboParent.DataTextField = "Name";
                    cboParent.DataValueField = "Id";
                    cboParent.DataBind();
                    switch (e.Item.ItemType)
                    {
                        case ListItemType.Item:
                        case ListItemType.AlternatingItem:
                            cboParent.SelectedValue = DataBinder.Eval(e.Item.DataItem, "ParentId").ToString();
                            break;
                    }
                    break;
            }
        }

        protected void grd_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "cmdAddNew":
                    using (var d = new Repository())
                    {
                        var cboParent = (DropDownList)e.Item.FindControl("cboParent");
                        var txtName = (TextBox)e.Item.FindControl("txtName");
                        var txtOrderDisplay = (TextBox)e.Item.FindControl("txtOrderDisplay");
                        var chkActive = (CheckBox)e.Item.FindControl("chkActive");
                        if (cboParent.SelectedIndex==0)
                            d.InsertTaxonomy(txtName.Text.Standard(), txtOrderDisplay.Text.ToInt(), chkActive.Checked, this.GetQueryString("").ToInt());
                        else
                            d.InsertTaxonomy(txtName.Text.Standard(), txtOrderDisplay.Text.ToInt(), chkActive.Checked, this.GetQueryString("").ToInt(),cboParent.SelectedValue.ToInt());
                    }
                    break;
                case "cmdUpdate":
                    using (var d = new Repository())
                    {
                        var cboParent = (DropDownList)e.Item.FindControl("cboParent");
                        var txtName = (TextBox)e.Item.FindControl("txtName");
                        var txtOrderDisplay = (TextBox)e.Item.FindControl("txtOrderDisplay");
                        var chkActive = (CheckBox)e.Item.FindControl("chkActive");
                        if (cboParent.SelectedIndex==0)
                            d.UpdateTaxonomy(e.CommandArgument.ToInt(),txtName.Text.Standard(), txtOrderDisplay.Text.ToInt(), chkActive.Checked);
                        else
                            d.UpdateTaxonomy(e.CommandArgument.ToInt(),txtName.Text.Standard(), txtOrderDisplay.Text.ToInt(), chkActive.Checked, cboParent.SelectedValue.ToInt());
                    }
                    break;
                case "cmdDelete":
                    using (var d = new Repository())
                    {
                        d.DeleteTaxonomy(e.CommandArgument.ToInt());
                    }
                    break;
            }
            LoadData();
        }

        protected void btnUpdateAll_Click(object sender, EventArgs e)
        {
            using (var d = new Repository())
            {
                foreach (DataGridItem item in grd.Items)
                {
                    var litId = (Literal) item.FindControl("litId");
                    var cboParent = (DropDownList)item.FindControl("cboParent");
                    var txtName = (TextBox)item.FindControl("txtName");
                    var txtOrderDisplay = (TextBox)item.FindControl("txtOrderDisplay");
                    var chkActive = (CheckBox)item.FindControl("chkActive");
                    if (cboParent.SelectedIndex == 0)
                        d.UpdateTaxonomy(litId.Text.ToInt(), txtName.Text.Standard(), txtOrderDisplay.Text.ToInt(), chkActive.Checked);
                    else
                        d.UpdateTaxonomy(litId.Text.ToInt(), txtName.Text.Standard(), txtOrderDisplay.Text.ToInt(), chkActive.Checked, cboParent.SelectedValue.ToInt());
                }
                LoadData();
            }
        }
    }
}